module service.RpcService;

import repository;
import model;
import client.MsgServerClient;
import msgtrans;
import hunt.logging;
import message.Constants;
import message.MsgDefine;
import std.conv : to;
import core.stdc.time;
import google.protobuf;
import std.array;
import bootstrap.DatabaseBootstrap;
import hunt.framework;
import comm.comm;

class RpcService {

   static int sendRedEnvelope(SendRedEnvelopeReq req, long[] split, ulong userId,long amount)
   {
      RedEnvelopeRepository redEnvelopeRepo = new RedEnvelopeRepository;
      RedEnvelopeSplitRepository redEnvelopeSplitRepo = new RedEnvelopeSplitRepository;
      UserAssetsRepository userAssetRepo = new UserAssetsRepository;
      auto em = defaultEntityManager();
      scope(exit)
      {
        em.close();
      }
      em.getTransaction().begin();
      try {
        int id = redEnvelopeRepo.addRedEnvelope(req,userId,em);
        userAssetRepo.updateUserAssets(amount - req.amount ,userId,em);
        foreach(long m ; split)
        {
          logInfo("split : %d", m);
          redEnvelopeSplitRepo.addRedEnvelopeSplit(req,userId,m,id,em);
        }
        em.getTransaction().commit();
        return id;
      }catch(Throwable e)
      {
        logErrorf("sendRedEnvelope error %s", e);
        em.getTransaction().rollback();
        return -1;
      }
   }

   static string lookRedEnvelope(int id,ulong userId,RedEnvelope redObj)
   {
       RedEnvelopeRecordRepository redEnvRecord = new RedEnvelopeRecordRepository;
       RedEnvelopeRecord[] records =  redEnvRecord.getAllById(id);
       return EncodeGetRedEnvelope(redObj,null,records,userId);
   }

   static string getRedEnvelope(int id,ulong userId,RedEnvelope redObj,UserAssets assets, out bool get)
   {
      RedEnvelopeSplitRepository  redEnvSplit = new RedEnvelopeSplitRepository;
      RedEnvelopeRecordRepository redEnvRecord = new RedEnvelopeRecordRepository;
      UserAssetsRepository  userAssetsRepo = new UserAssetsRepository;
      RedEnvelopeRepository r = new RedEnvelopeRepository;
      RedEnvelopeSplit splitObj =  redEnvSplit.getOne(id);
      if (splitObj is null)
      {
        r.updateStatus(id);
        ErrorObj obj;
        import message.MsgDefine;
        obj.code = ErrorCode.ERR_FINISHED;
        obj.message = "红包已经领完了";
        return EncodeJsonValue(obj);
      }else
      {
        auto em = defaultEntityManager();
        scope(exit)
        {
          em.close();
        }
        em.getTransaction().begin();
        try {
          {
            userAssetsRepo.updateUserAssets(assets.assets + splitObj.amount,userId,em);
            redEnvSplit.deleRedEnvelopeSplit(splitObj,em);
            redEnvRecord.insertRecord(redObj,splitObj,userId,em);
            get = true;
          }
          em.getTransaction().commit();
          RedEnvelopeRecord[] records =  redEnvRecord.getAllById(redObj.id);
          return EncodeGetRedEnvelope(redObj,splitObj,records,userId);
        }catch(Throwable e)
        {
          em.getTransaction().rollback();
          ErrorObj obj;
          import message.MsgDefine;
          obj.code = ErrorCode.ERR_DB;
          get = false;
          return EncodeJsonValue(obj);
        }
      }
   }


}

